package com.centling.smartSealForPhone.fragment

import android.content.Intent
import android.support.v7.app.AlertDialog
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import com.centling.smartSealForPhone.BuildConfig
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.activity.*
import com.centling.smartSealForPhone.constants.GlobalData
import com.centling.smartSealForPhone.constants.SPConstants
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.constants.UserType
import com.centling.smartSealForPhone.utils.*
import kotlinx.android.synthetic.main.fragment_seal_manager.*
import org.json.JSONObject
import java.util.*

class SealManageFragment : TitleBarFragment(), View.OnClickListener {

    override fun setLayoutResource(): Int {
        return R.layout.fragment_seal_manager
    }

    override fun findViewInThisFunction(rootView: View) {
        setTitleBarText("用印管理")
        setTitleBarVisibility(View.GONE)
        initView()
    }

    private fun initView() {
        GlideApp.with(mContext).load(GlobalData.appImageMain).placeholder(R.drawable.iv_device_banner).into(iv_home_banner)

        iv_home_banner.setOnClickListener {
            if (BuildConfig.DEBUG) {
                val editText = EditText(mContext)
                AlertDialog.Builder(mContext).setTitle("请输入测试字符串").setView(editText).setPositiveButton("发送") { dialog, _ ->
                    dialog.dismiss()
                    try {
                        when(editText.text.toString()){
                            "clearAll" ->{
                                ControlUtil().clearAll()
                                ShowToast.shortTime("总清")
                            }
                            "fetchKey" -> {
                                ControlUtil().fetchKey()
                                ShowToast.shortTime("获取KEY")
                            }
                            "fetchSn" -> {
                                ControlUtil().getSN()
                                ShowToast.shortTime("获取SN")
                            }
                            "setSn" -> {
                                ControlUtil().setSN("6321A1507320101")
                                ShowToast.shortTime("获取SN")
                            }
                        }
                    } catch (ignored: Exception) {

                    }
                }.setNegativeButton("取消") { dialog, _ ->
                    dialog?.dismiss()
                }.show()
            }
        }

        if (TextUtils.equals(UserType.EQUIP_OA_USER, SPUtil.getString(SPConstants.USER_TYPE))) {
            tv_home_apply.text = "流程码"
        } else {
            tv_home_apply.text = "申请"
        }

        ll_home_apply.setOnClickListener(this)
        rl_home_seal.setOnClickListener(this)
        rl_home_review.setOnClickListener(this)
        ll_home_history.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        sendRequestToFetchUnread()
    }

    private fun sendRequestToFetchUnread() {
        getEnqueue(String.format(Locale.CHINA, UrlConstants.FETCH_UNREAD_MSG),
                object : OkHttpUtil.NetCallBack {
                    override fun onSucceed(json: String) {
                        val jsonObject = JSONObject(json)
                        dealWithUnreadMsg(jsonObject.optString("checkedCount").toInt(),jsonObject.optString("reviewFailedCount").toInt())
                    }

                    override fun onFailed(reason: String) {

                    }
                })
    }

    private fun dealWithUnreadMsg(checkedCount:Int,reviewFailedCount:Int) {
        if (checkedCount > 0) {
            seal_manage_stamp_num?.visibility = View.VISIBLE
            if (checkedCount > 99) {
                seal_manage_stamp_num?.text = ".."
            } else {
                seal_manage_stamp_num?.text = "$checkedCount"
            }
        } else {
            seal_manage_stamp_num?.visibility = View.GONE
        }

        if (reviewFailedCount > 0) {
            seal_manage_review_num?.visibility = View.VISIBLE
            if (reviewFailedCount > 99) {
                seal_manage_review_num?.text = ".."
            } else {
                seal_manage_review_num?.text = "$reviewFailedCount"
            }
        } else {
            seal_manage_review_num?.visibility = View.GONE
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ll_home_apply -> {
                if (TextUtils.equals(UserType.EQUIP_OA_USER, SPUtil.getString(SPConstants.USER_TYPE))) {
                    if (TextUtils.equals("1", SPUtil.getString(SPConstants.ENABLE_APP_PULL))) {
                        ShowDialog.showEditDialog(mContext, "输入流程码", "") { view ->
                            val applySn = view.tag as String;
                            if (TextUtils.isEmpty(applySn)) {
                                ShowToast.shortTime("请输入流程码")
                                return@showEditDialog;
                            }
                            startActivity(Intent(mContext, StampConfirmActivity::class.java)
                                    .putExtra("sealApplyId", applySn))
                        }
                    } else {
                        ShowToast.shortTime("没有权限")
                    }
                } else {
                    startActivity(Intent(activity, ApplySealActivity::class.java))
                }
            }
            R.id.rl_home_seal -> {
                if (GlobalData.isInFixMode) {
                    ShowToast.shortTime("设备当前处于维修模式")
                } else {
                    startActivity(Intent(activity, StampListActivity::class.java))
                }
            }
            R.id.rl_home_review -> startActivity(Intent(activity, ReviewActivity::class.java))
            R.id.ll_home_history -> startActivity(Intent(activity, HistoryActivity::class.java))
        }
    }
}



